package com.example.repair

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.RepairBeanItem
import com.example.base.mvi.BaseActivity
import com.example.base.mvi.RequestType
import com.example.base.mvi.UiState
import com.example.repair.databinding.ActivityRepairApproveBinding
import com.example.repair.databinding.ActivityRepairBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/repair/RepairApproveActivity")
@AndroidEntryPoint
class RepairApproveActivity : BaseActivity<ActivityRepairApproveBinding, RepairIntent, RepairViewModel>() {
    override val binding by lazy {
        ActivityRepairApproveBinding.inflate(layoutInflater)
    }
    override val vm by viewModels<RepairViewModel>()

    @Autowired
//    lateinit var repair:RepairBeanItem
    lateinit var rid:String
    override fun initView() {
        ARouter.getInstance().inject(this)
        vm.sendIntent(RepairIntent.getRepirById(rid.toInt()))

        binding.repDetailOk.setOnClickListener {
            binding.bean?.processingStatus = "处理中"
            vm.sendIntent(RepairIntent.updataRepair(rid.toInt(), binding.bean!!))
        }

        binding.repDetailRefause.setOnClickListener {
            binding.bean?.processingStatus = "已完成"
            vm.sendIntent(RepairIntent.updataRepair(rid.toInt(), binding.bean!!))
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when (it.type) {
                            RequestType.UPDATE -> {
                                ToastUtils.showLong("审批完成")
                                finish()
                            }

                            RequestType.DEFAULT -> {
                                val item = it.data as RepairBeanItem
                                binding.bean = item
                                if (item.processingStatus != "已完成") {
                                    binding.repDetailGroup.visibility = View.VISIBLE
                                }
                                if (item.processingStatus == "处理中") {
                                    binding.repDetailProCircle2.setBackgroundResource(R.drawable.shape_solid_darkgreen)
                                } else if (item.processingStatus == "已完成") {
                                    binding.repDetailProCircle2.setBackgroundResource(R.drawable.shape_solid_darkgreen)
                                    binding.repDetailProCircle3.setBackgroundResource(R.drawable.shape_solid_darkgreen)
                                }
                            }

                            else -> {}
                        }

                    }

                    is UiState.onError -> {
                        Log.i("===", it.msg)
                    }

                    else -> {}
                }
            }
        }
    }
}